package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.dto.mumber.ProjectPageDTO;
import com.zzyl.nursing.mapper.ProjectMapper;
import com.zzyl.nursing.service.ProjectService;
import com.zzyl.nursing.vo.mumber.ProjectPageVO;
import com.zzyl.serve.domain.NursingProject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 24 5月 2025 01:43
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, NursingProject> implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 1.1 分页查询护理项目列表
     *
     * @param projectPageDTO
     * @return
     */
//    @Override
//    public List<ProjectPageVO> page(ProjectPageDTO projectPageDTO) {
//        List<NursingProject> list = this.lambdaQuery().eq(projectPageDTO.getStatus() != null, NursingProject::getStatus, projectPageDTO.getStatus())
//                .like(projectPageDTO.getName() != null && !StrUtil.isBlank(projectPageDTO.getName()),
//                        NursingProject::getName, projectPageDTO.getName())
//                .list();
//
//        List<ProjectPageVO> collect = list.stream().map(o -> {
//            ProjectPageVO projectPageVO = new ProjectPageVO();
//            BeanUtils.copyProperties(o, projectPageVO);
//            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            //将时间转换成字符串
//            projectPageVO.setCreateTime(o.getCreateTime().format(dtf));
//            return projectPageVO;
//        }).collect(Collectors.toList());
//        return collect;
//    }


    /**
     * 1.1 分页查询护理项目列表
     *
     * @param projectPageDTO
     * @return
     */
    @Override
    public TableDataInfo<ProjectPageVO> pageList(ProjectPageDTO projectPageDTO) {
        Page<NursingProject> page = new Page<>(projectPageDTO.getPageNum(), projectPageDTO.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        Page<NursingProject> page1 = this.lambdaQuery().eq(projectPageDTO.getStatus() != null, NursingProject::getStatus, projectPageDTO.getStatus()).like(projectPageDTO.getName() != null && !StrUtil.isBlank(projectPageDTO.getName()), NursingProject::getName, projectPageDTO.getName()).page(page);
        List<ProjectPageVO> collect = page1.getRecords().stream().map(o -> {
            ProjectPageVO projectPageVO = new ProjectPageVO();
            BeanUtils.copyProperties(o, projectPageVO);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //将时间转换成字符串
            projectPageVO.setCreateTime(o.getCreateTime().format(dtf));
            if (o.getUpdateTime() != null) {
                projectPageVO.setUpdateTime(o.getUpdateTime().format(dtf));
            }
            return projectPageVO;
        }).collect(Collectors.toList());
        TableDataInfo<ProjectPageVO> projectPageVOTableDataInfo = new TableDataInfo<>(collect, (int) page1.getTotal());
        projectPageVOTableDataInfo.setCode(200);
        projectPageVOTableDataInfo.setMsg("查询成功");
        return projectPageVOTableDataInfo;
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public ProjectPageVO getProjectById(Long id) {
        NursingProject byId = this.getById(id);
        ProjectPageVO projectPageVO = new ProjectPageVO();
        BeanUtils.copyProperties(byId, projectPageVO);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //将时间转换成字符串
        projectPageVO.setCreateTime(byId.getCreateTime().format(dtf));
        if (byId.getUpdateTime() != null) {
            projectPageVO.setUpdateTime(byId.getUpdateTime().format(dtf));
        }
        return projectPageVO;
    }
}
